package com.zlx.redismessage.task;

import com.alibaba.fastjson.JSON;
import com.zlx.redismessage.applicationContext.ApplicationContextHolder;
import com.zlx.redismessage.conf.SystemCfg;
import com.zlx.redismessage.model.User;
import com.zlx.redismessage.producer.BaseProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @PackageName: com.jingtoo.detection.common.task
 * @ClassName: DetectionTask
 * @Author: gaoYuan
 * @Date: 2022/5/6 17:35
 * @Description: DetectionTask 检测任务入口,用来分发具体的检测任务.
 */
public class DetectionTask extends ScheduleTask {

    private static final Logger logger = LoggerFactory.getLogger(DetectionTask.class);
    public static final String REDIS_PREFIX = "dzzz:push:task:";
	private static final BaseProducer baseProducer;

	static {
		baseProducer = ApplicationContextHolder.getBean(BaseProducer.class);
	}
    public DetectionTask(String id) {
        super(id);
    }

    @Override
    public void run() {
        logger.info("检测任务初始化...");
        String templateId = super.getId();
		List<User> users = new ArrayList<>();
		for (int i = 0; i < 100000; i++) {
			User user = new User();
			user.setId(UUID.randomUUID().toString().replace("-", ""));
			user.setAddress("南京" + i);
			user.setFlag("1");
			user.setName("噜噜噜噜" + i);
			users.add(user);
		}
		doReleaseTask(users);
		try {
			Long interval = SystemCfg.interval;
			Thread.sleep(interval.intValue());
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
    }


	/**
     * 发布任务
     *
     * @param Users 任务集合
     */
    private void doReleaseTask(List<User> Users) {
        for (User User : Users) {
            baseProducer.sendMsg("DZZZ", JSON.toJSONString(User));
        }
    }


}
